//个人填报功能


import React, {PureComponent} from "react";

import {Input, Row, Progress, Col, Card, Popover, Button} from 'antd';

import {calculate, parse} from '../calc';
import FormItemLabel from "antd/es/form/FormItemLabel";
import 'echarts/lib/chart/line';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

import ReactEcharts from "echarts-for-react";
import SelectSearch from "@/pages/RadarChart/base/SelectSearch";
import {
  format2Letters,
  formatScore,
  getAbilityById,
  getIndicatorById,
  getJobById,
  guid,
  listByAbility,
  listByJob, listScore
} from "@/pages/RadarChart/base/DataLoad";
import Form from "antd/es/form";
import RadarChartView from "@/pages/RadarChart/components/RedarChartView";
import {stringify} from "qs";
import Text from "antd/es/typography/Text";
import NumInput from "@/pages/RadarChart/base/NumInput";


export default class PersonalReport extends PureComponent {
  state = {
    jobs: [],
    value: [],
    data: [],
    valueMap: {}
  }


  analyseData() {


    let jobs1 = this.state.jobs;

    let indicator = [];
    let ability = [];
    let abilityMap = {};
    //指标-> 能力 -> 职位
    let set = new Set();
    for (let job of jobs1) {
      let rule = job.rule;
      let base = job.base;
      let important = job.important;
      if (base == null) {
        base = {};
      }
      if (important == null) {
        important = [];
      }
      if (rule) {
        for (let aId in rule) {

          if (abilityMap[aId] == null) {
            let abilityById = getAbilityById(aId);
            abilityMap[aId] = {abilityId: aId, name: abilityById.name, rule: {}}
            if (abilityById.rule) {
              abilityMap[aId].rule = abilityById.rule;
            }
          }
          if (abilityMap[aId].base == null) {
            abilityMap[aId].base = {};
          }
          if (abilityMap[aId].weight == null) {
            abilityMap[aId].weight = {}
          }
          if (abilityMap[aId].important == null) {
            abilityMap[aId].important = {}
          }

          if (important.indexOf(aId) != -1) {
            abilityMap[aId].important[job.jobId] = true;
          }
          if (base[aId] == null) {
            base[aId] = 60;
          }
          if (abilityMap[aId].max == null || abilityMap[aId].max > base[aId]) {
            abilityMap[aId].max = base[aId];
          }

          abilityMap[aId].weight[job.jobId] = rule[aId];
          abilityMap[aId].base[job.jobId] = base[aId];
        }
      }


    }
    let indicatorMap = {};
    for (let a in abilityMap) {
      let temp = abilityMap[a];
      let sum = 0;
      let weightSum = 0;
      for (let r in temp.rule) {
        sum += (this.state.valueMap[r] == null ? 0 : this.state.valueMap[r]) * temp.rule[r];
        weightSum += temp.rule[r]
        if (!set.has(r)) {
          let item = getIndicatorById(r);
          indicatorMap[r] = item;
          item.max = temp.max + 20 > 100 ? 100 : temp.max + 20;
          item.from = [];
          indicator.push(item);

          set.add(r);
        }
        //添加会影响到的能力
        indicatorMap[r].from.push(temp.name);
      }
      temp.sum = new Number(sum / weightSum).toFixed(2);
      ability.push(temp);
    }
    return {indicator: indicator, ability: ability}
  }


  getJobsFromValue(value) {
    let array = value;
    let jobs = [];
    for (let a of array) {
      let job = getJobById(a);
      if (job) {
        jobs.push(job);
      }
    }
    return jobs;
  }


  render() {
    const {indicator, ability} = this.analyseData();
    let abvMap = {}
    for (let a of ability) {
      abvMap[a.abilityId] = a.sum;
    }
    return <div>
      <Card>
        <Row>
          <Col span={8}>
            搜索职位:&nbsp;
            <SelectSearch
              key={guid()}
              mark={""}
              onChange={(value, data, type) => {
                if (type != "init") {
                  let jobsFromValue = this.getJobsFromValue(value);

                  this.setState({jobs: jobsFromValue, value: value, data: data});
                } else {

                }
              }}
              defaultValue={this.state.value}
              data={this.state.data}
              load={(value, call) => {
                let array = listByJob(value);

                call(array);
              }}
              style={{minWidth: "20em"}}
              defaultValue={this.state.value}
              mode={"multiple"}
            ></SelectSearch>
          </Col>
        </Row>
      </Card>
      <Card>
        {indicator.length != 0 ? <Button style={{marginLeft: "1.8em"}} onClick={() => {
          this.setState({valueMap: {}})
        }}>重置分数</Button> : ""}
        <Row style={{margin: "0.3em"}}>
          {indicator.map((item, index) => {
              let line = `<p>影响能力:${JSON.stringify(item.from).replace("\"", "")}</p>`;
              let line1 = `<div>打分标准:${item.description}</div>`;
              let html = line + line1;

              return <Col span={6} key={index}>
                <Row gutter={24} style={{margin: "0.3em"}}>

                  <Col span={24}>
                    <Popover placement="topLeft" content={<div dangerouslySetInnerHTML={{
                      __html: html
                    }}></div>}
                             trigger="click">
                      {item.name}: <SelectSearch
                      key={guid()}
                      style={{width: "6em"}}
                      mark={item} onChange={(value, mark, type) => {

                      let v = new Number(value).toFixed(0);

                      let indicatorId = item.indicatorId;
                      let obj = {};
                      for (let i in this.state.valueMap) {
                        obj[i] = this.state.valueMap[i];
                      }
                      obj[indicatorId] = v;
                      console.log(obj);
                      if (type != "init") {
                        this.setState({valueMap: obj})
                      } else {
                        this.state.valueMap = obj;
                      }

                    }} load={(value, call) => {
                      let array = listScore();

                      call(array);
                    }}

                      data={listScore()}
                      defaultValue={this.state.valueMap[item.indicatorId] == null ? null : "" + this.state.valueMap[item.indicatorId]}
                      mark={item}
                    ></SelectSearch>
                    </Popover>
                    {/*<NumInput min={0} data={item} type={"number"} max={item.max}*/}
                    {/*          value={this.state.valueMap[item.name] == null ? 0 : this.state.valueMap[item.name]}*/}
                    {/*          addonBefore={item.name + " [0-" + item.max + "]"} onChange={(v, data) => {*/}
                    {/*  let indicatorId = data.indicatorId;*/}
                    {/*  let obj = {};*/}
                    {/*  for (let i in this.state.valueMap) {*/}
                    {/*    obj[i] = this.state.valueMap[i];*/}
                    {/*  }*/}
                    {/*  obj[indicatorId] = v;*/}

                    {/*  this.setState({valueMap: obj})*/}
                    {/*}}/>*/}
                  </Col>

                </Row>

              </Col>
            }
          )}
        </Row>
      </Card>
      <Card>

        <Row>
          <Col span={9}>
            {ability.map((item, index) => {

              let str = "";
              let sumWeight = 0;
              for (let i in item.rule) {

                let name = getIndicatorById(i).name;
                let weight = item.rule[i];
                str += "+" + name + "*" + weight;
                sumWeight += weight;
              }
              if (str != "") {
                str = str.substr(1, str.length);
                str = "(" + str + ")/" + sumWeight;
              }
              let html = `<p>计算公式:${str}</p>`;

              for (let i in item.base) {
                let job = getJobById(i);
                html += `<p >合格分数[${job.name}]:${format2Letters(item.base[i])} [${item.base[i] < item.sum ? "合格" : "不合格"}]</p>`;
              }


              return <Popover placement="top" content={<div dangerouslySetInnerHTML={{__html: html}}></div>}
                              trigger="click">

                <Text strong
                      style={"{}" == JSON.stringify(item.important) ? {} : {color: "red"}}>{item.name} :[得分: {format2Letters(item.sum)}]</Text>
                <br/>
              </Popover>
            })}
          </Col>
          <Col span={15}>
            <RadarChartView job={this.state.jobs} valueMap={abvMap}></RadarChartView>
          </Col>
        </Row>
      </Card>
    </div>;
  }
}




